using System.IdentityModel.Tokens.Jwt;
using Admin.RazorLib;
using BaseData.RazorLib;
using Blazored.LocalStorage;
using Client.BlazorWasmApp.Authentication;
using Client.BlazorWasmApp.Caches;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Production.RazorLib;
using Radzen;
using Swimj.RazorLib.Caches;

namespace Client.BlazorWasmApp
{
    internal class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);
            builder.RootComponents.Add<App>("#app");
            builder.RootComponents.Add<HeadOutlet>("head::after");

            builder.Services.AddScoped<DialogService>();
            builder.Services.AddScoped<NotificationService>();
            builder.Services.AddScoped<TooltipService>();
            builder.Services.AddScoped<ContextMenuService>();
            //builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

            // 添加缓存
            builder.Services.AddBlazoredLocalStorage();
            builder.Services.AddScoped<ISecureCache, LocalSecureCache>();

            builder.Services.AddOptions();
            builder.Services.AddAuthorizationCore();

            builder.Services.AddScoped<JwtSecurityTokenHandler>();
            builder.Services.AddScoped<CustomAuthStateProvider>();
            builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
            builder.Services.AddTransient<AuthHeaderHandler>();

            builder.Services.AddRadzenComponents();

            builder.Services
                .AddHttpClient("adminApi", h =>
                {
                    h.BaseAddress = new Uri("http://localhost:5100");
                    // h.BaseAddress = new Uri("https://admin.webapi");
                }).AddHttpMessageHandler<AuthHeaderHandler>();

            builder.Services
                .AddHttpClient("basedataApi", c =>
                {
                    c.BaseAddress = new Uri("http://localhost:5200");
                    // c.BaseAddress = new Uri("http://dataserver.webapi");
                })
                .AddHttpMessageHandler<AuthHeaderHandler>();

            builder.Services
                .AddHttpClient("productionApi", c =>
                {
                    c.BaseAddress = new Uri("http://localhost:5300");
                    // c.BaseAddress = new Uri("http://dataserver.webapi");
                })
                .AddHttpMessageHandler<AuthHeaderHandler>();

            // builder.Services
            //     .AddHttpClient("dataserverApi", c =>
            //     {
            //         c.BaseAddress = new Uri("http://localhost:5200");
            //         // c.BaseAddress = new Uri("http://dataserver.webapi");
            //     })
            //     .AddHttpMessageHandler<AuthHeaderHandler>();

            //services.AddHttpClientInterceptor();

            builder.Services.AddAdminBlazor();
            builder.Services.AddBaseDataBlazor();
            builder.Services.AddProductionBlazor();
            // builder.Services.AddDataServerBlazor();

            await builder.Build().RunAsync();
        }
    }
}